home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_amanda.idb / usr / freeware / libexec / chg-multi.z / chg-multi
Encoding:
Text File  |  1999-07-16  |  7.6 KB  |  335 lines

  1. #!/bin/sh
  2. #
  3. # Amanda, The Advanced Maryland Automatic Network Disk Archiver
  4. # Copyright (c) 1991-1998 University of Maryland at College Park
  5. # All Rights Reserved.
  6. #
  7. # Permission to use, copy, modify, distribute, and sell this software and its
  8. # documentation for any purpose is hereby granted without fee, provided that
  9. # the above copyright notice appear in all copies and that both that
  10. # copyright notice and this permission notice appear in supporting
  11. # documentation, and that the name of U.M. not be used in advertising or
  12. # publicity pertaining to distribution of the software without specific,
  13. # written prior permission.  U.M. makes no representations about the
  14. # suitability of this software for any purpose.  It is provided "as is"
  15. # without express or implied warranty.
  16. #
  17. # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
  19. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  20. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  21. # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  22. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23. #
  24. # Author: James da Silva, Systems Design and Analysis Group
  25. #               Computer Science Department
  26. #               University of Maryland at College Park
  27. #
  28.  
  29. #
  30. # chg-multi.sh - generic tape changer script
  31. #
  32.  
  33. prefix=/usr/freeware
  34. exec_prefix=${prefix}
  35. libexecdir=/usr/freeware/libexec
  36.  
  37. pname="chg-multi"
  38.  
  39. PATH=$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
  40. export PATH
  41.  
  42. USE_VERSION_SUFFIXES="no"
  43. if test "$USE_VERSION_SUFFIXES" = "yes"; then
  44.     SUF="-2.4.1p1"
  45. else
  46.     SUF=
  47. fi
  48.  
  49. ourconf=`getconf$SUF changerfile`
  50.  
  51. MT=/sbin/mt
  52. MTF=-f
  53.  
  54. # read in some config parameters
  55.  
  56. if [ \! -f $ourconf ]; then
  57.     echo "<none> $pname: $ourconf does not exist"
  58.     exit 2
  59. fi
  60.  
  61. firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
  62. if [ "$firstslot" = "" ]; then
  63.     echo "<none> $pname: firstslot not specified in $ourconf"
  64.     exit 2
  65. fi
  66.  
  67. lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
  68. if [ "$lastslot" = "" ]; then
  69.     echo "<none> $pname: lastslot not specified in $ourconf"
  70.     exit 2
  71. fi
  72.  
  73. nslots=`expr $lastslot - $firstslot + 1`
  74.  
  75. gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
  76. if [ "$gravity" = "" ]; then
  77.     echo "<none> $pname: gravity not specified in $ourconf"
  78.     exit 2
  79. fi
  80.  
  81. needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
  82. if [ "$needeject" = "" ]; then
  83.     echo "<none> $pname: needeject not specified in $ourconf"
  84.     exit 2
  85. fi
  86.  
  87. multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
  88. if [ "$multieject" = "" ]; then
  89.     echo "<none> $pname: multieject not specified in $ourconf"
  90.     multieject=0
  91. #    exit 2
  92. fi
  93.  
  94. ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
  95. if [ "$ejectdelay" = "" ]; then
  96.     echo "<none> $pname: ejectdelay not specified in $ourconf"
  97.     ejectdelay=0
  98. fi
  99.  
  100. ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
  101. if [ "$ourstate" = "" ]; then
  102.     echo "<none> $pname: statefile not specified in $ourconf"
  103.     exit 2
  104. fi
  105.  
  106. # read in state: only curslot and curloaded at the present time
  107.  
  108. curslot=`awk '$1 == "curslot" {print $2}' $ourstate 2>/dev/null`
  109. if [ "$curslot" = "" ]; then
  110.     curslot=$firstslot
  111. fi
  112.  
  113. curloaded=`awk '$1 == "curloaded" {print $2}' $ourstate 2>/dev/null`
  114. if [ "$curloaded" = "" ]; then
  115.     curloaded=0
  116. fi
  117.  
  118.  
  119. # process the command-line
  120.  
  121. # control vars to avoid code duplication: not all shells have functions!
  122. usage=0
  123. checkgravity=0
  124. ejectslot=0
  125. loadslot=0
  126. slotempty=0
  127.  
  128. if [ $# -ge 1 ]; then command=$1; else command="-usage"; fi
  129.  
  130. case "$command" in
  131.  
  132. -info) # return basic information about changer
  133.  
  134.     backwards=`expr 1 - $gravity`
  135.     echo $curslot $nslots $backwards
  136.     exit 0
  137.     ;;
  138.  
  139. -reset) # reset changer
  140.  
  141.     checkgravity=0
  142.     loadslot=1
  143.     newslot=$firstslot
  144.  
  145.     # XXX put changer-specific reset here, if applicable
  146.     ;;
  147.  
  148. -eject) # eject tape if loaded
  149.  
  150.     checkgravity=0
  151.     loadslot=0
  152.     newslot=$curslot
  153.     ejectslot=1
  154.  
  155.     if [ $curloaded -eq 0 ]; then
  156.         echo $curslot "slot already empty"
  157.         exit 1
  158.     fi
  159.     ;;
  160.  
  161. -slot)    # change to slot
  162.  
  163.     checkgravity=1
  164.     loadslot=1
  165.  
  166.     slotparm=$2
  167.     case "$slotparm" in
  168.     [0-9]*)    
  169.         newslot=$slotparm
  170.         if [ \( $newslot -gt $lastslot \) -o \
  171.              \( $newslot -lt $firstslot \) ]; then
  172.             echo $newslot "no slot $newslot: legal range is" \
  173.                 "$firstslot ... $lastslot"
  174.             exit 1
  175.         fi
  176.         ;;
  177.     current)
  178.         newslot=$curslot
  179.         ;;
  180.     first)
  181.         newslot=$firstslot
  182.         ;;
  183.     last)
  184.         newslot=$lastslot
  185.         ;;
  186.     next|advance)
  187.         newslot=`expr $curslot + 1`
  188.         if [ $newslot -gt $lastslot ]; then
  189.             newslot=$firstslot
  190.         fi
  191.         if [ $slotparm = advance ]; then
  192.             loadslot=0
  193.         fi
  194.         ;;
  195.     prev)
  196.         newslot=`expr $curslot - 1`
  197.         if [ $newslot -lt $firstslot ]; then
  198.             newslot=$lastslot
  199.         fi
  200.         ;;
  201.     *)
  202.         echo "<none> bad slot name \"$slotparm\""
  203.         exit 1
  204.         ;;
  205.     esac
  206.     ;;
  207. *)
  208.     usage=1
  209.     ;;
  210. esac
  211.  
  212.  
  213. if [ $usage -eq 1 ]; then
  214.     echo "<none> usage: $pname {-reset | -slot [<slot-number>|current|next|prev|advance]}"
  215.     exit 2
  216. fi
  217.  
  218.  
  219. # check for legal move
  220.  
  221. if [ \( $checkgravity -eq 1 \) -a \( $gravity -ne 0 \) ]; then
  222.     if [ \( $newslot -lt $curslot \) -o \( "$slotparm" = "prev" \) ]
  223.     then
  224.         echo "$newslot cannot go backwards in gravity stacker"
  225.         exit 1
  226.     fi
  227. fi
  228.  
  229. # get tape device name
  230.  
  231. device=`awk '$1 == "slot" && $2 == '$newslot' {print $3}' $ourconf 2>/dev/null`
  232. if [ "$device" = "" ]; then
  233.     echo "$newslot $pname: slot $newslot device not specified in $ourconf"
  234.     exit 2
  235. fi
  236.  
  237. # check if load needs an eject first
  238.  
  239. if [ \( $needeject -eq 1 \) -a \( $loadslot -eq 1 \) -a \
  240.      \( $curloaded -eq 1 \) -a \( $newslot -ne $curslot \) ]; then
  241.     ejectslot=1
  242. fi
  243.  
  244.  
  245. if [ $ejectslot -eq 1 ]; then    # eject the tape from the drive
  246.  
  247.     # XXX put changer-specific load command here, if applicable
  248.  
  249.     curloaded=0        # unless something goes wrong
  250.     slotempty=0
  251.  
  252.     # generically, first check that the device is there
  253.  
  254.     if [ ! -c $device ]; then
  255.         echo "$newslot $device: not a device file"
  256.         exit 2
  257.     fi
  258.  
  259.     # if multiple eject is required, do it now
  260.     if [ $multieject -eq 1 ]; then
  261.         loopslot=$curslot
  262.         while [ $loopslot -lt $newslot ]; do
  263.             $MT $MTF $device offline >/dev/null 2>&1
  264.             if [ $? -ne 0 ]; then
  265.                 echo "$newslot $device: unable to change slot $loopslot"
  266.                 exit 2
  267.             fi
  268.             loopslot=`/usr/bin/expr $loopslot + 1`
  269.         done
  270.     fi
  271.   
  272.     # second, try to unload the device
  273.     $MT $MTF $device offline >/dev/null 2>&1
  274.     if [ $? -ne 0 ]; then
  275.         #
  276.         # XXX if the changer-specific eject command can distinguish
  277.         # betweeen "slot empty" and more serious errors, return 1
  278.         # for the first case, 2 for the second case.  Generically,
  279.         # we just presume an error signifies an empty slot.
  280.         #
  281.         slotempty=1
  282.     else
  283.         sleep $ejectdelay
  284.     fi
  285. fi
  286.  
  287. if [ $loadslot -eq 1 ]; then    # load the tape from the slot
  288.  
  289.     # XXX put changer-specific load command here, if applicable
  290.  
  291.     curloaded=1        # unless something goes wrong
  292.     slotempty=0
  293.  
  294.     # generically, first check that the device is there
  295.  
  296.     if [ ! -c $device ]; then
  297.         echo "$newslot $device: not a device file"
  298.         exit 2
  299.     fi
  300.  
  301.     # second, try to rewind the device
  302.     $MT $MTF $device rewind >/dev/null 2>&1
  303.     if [ $? -ne 0 ]; then
  304.         #
  305.         # XXX if the changer-specific load command can distinguish
  306.         # betweeen "slot empty" and more serious errors, return 1
  307.         # for the first case, 2 for the second case.  Generically,
  308.         # we just presume an error signifies an empty slot.
  309.         #
  310.         slotempty=1
  311.         curloaded=0
  312.     fi
  313. fi
  314.  
  315. # update state
  316.  
  317. echo "# multi-changer state cache: DO NOT EDIT!" >  $ourstate
  318. echo curslot $newslot                  >> $ourstate
  319. echo curloaded $curloaded             >> $ourstate
  320.  
  321. # return slot info
  322.  
  323. if [ $slotempty -eq 1 ]; then
  324.     echo "$newslot slot is empty"
  325.     exit 1
  326. fi
  327.  
  328. if [ "$command" = -slot -a "$slotparm" = advance ]; then
  329.     device=/dev/null
  330. fi
  331.  
  332. echo $newslot $device
  333.  
  334. exit 0
  335.